/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.event;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itgrail.grail.ddd.annotation.event.EventHandler;
import com.itgrail.grail.ddd.event.EventHandlerI;
import com.itgrail.pilot.domain.user.entity.UserE;
import com.itgrail.pilot.domain.user.event.UpdateUserEvent;
import com.itgrail.pilot.domain.user.service.UserService;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.UserMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.UserDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.itgrail.grail.dto.Response;
import com.itgrail.grail.utils.reflect.BeanMapper;

@EventHandler
@Slf4j
public class UpdateUserEventHandler implements EventHandlerI<Response, UpdateUserEvent> {

    @Autowired
    private UserMapper mapper;

    @Autowired
    private UserService userService;

    @Override
    public Response execute(UpdateUserEvent updateUserEvent) {
        log.info("修改用户信息:{}", JSON.toJSONString(updateUserEvent));
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", updateUserEvent.getId());
        UserDO userDO = mapper.selectOne(queryWrapper);
        if (userDO != null) {
            UserE userE = BeanMapper.map(userDO, UserE.class);
            userService.updateUser(userE);
        }
        return Response.buildSuccess();
    }
}
